package parser;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ParserMethods {
	
	public static String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			if (el.hasChildNodes()){
				textVal = el.getFirstChild().getNodeValue();
			}else
				if (textVal==null){
					textVal="";
				}
		}
		return textVal;
	}

	public static String replace(String text, String from, String to){
		StringBuffer sb = new StringBuffer();
		int fromPos = text.indexOf(from);
		int startPos =0;
		int fromLength = from.length();
		while(fromPos!=-1){
			sb.append(text.substring(startPos,fromPos)).append(to);
			startPos = fromPos+fromLength;
			fromPos = text.indexOf(from, startPos);
		}
		sb.append(text.substring(startPos,text.length()));
		return sb.toString();
	}

}
